
package web;

import java.util.List;

import model.User;

import com.opensymphony.xwork2.Action;

import exceptions.LeagueRestrictionException;
import exceptions.UserNotFoundException;

/**
 * Class for manipulates actions of the user in web application
 * @author Andre Melo
 * @author Marcus Leite 
 * @author Sergio Danilo
 * 
 */
public class UserAction extends BLMSAction {

	private static final long serialVersionUID = -1742193426253248370L;

	//Attributes
	private User user;
	
	private Integer id;
	
	private String email;
	
	private List<User> users;

	/**
	 * @return Success or fail in application
	 */
	public String main() {
		return Action.SUCCESS;
	}
	
	/**
	 * @return The input application
	 */
	public String input() {
		return Action.INPUT;
	}
	
	/**
	 * Save the application
	 * 
	 * @return All users
	 */
	public String save() {
		try {
			if (this.user.getId() == null) {
				System.out.println("Saving new user");
				userService.save(user);
			} else {
				System.out.println("Updating");
				userService.update(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Action.INPUT;
		}
		user = new User();
		return listAll();
	}
	
	public String editMe(){
		
		userService.update(user);
		return Action.SUCCESS;
	}
	
	/**
	 * Delete the user action application
	 * @return Success or fail
	 * @throws LeagueRestrictionException
	 */
	public String delete() throws LeagueRestrictionException {
		userService.delete(id);
		return listAll();
	}
	
	/**
	 * List all users
	 * @return Success or fail
	 */
	public String listAll() {
		users = userService.listAll();
		return Action.SUCCESS;
	}
	
	public String findById() {
		user = userService.findById(id);
		return Action.SUCCESS;
	}

	public String findByEmail() {
		try {
			user = userService.findByEmail(email);
		} catch (UserNotFoundException e) {
			return Action.ERROR;
		}
		return Action.SUCCESS;
	}
	
	public String details(){
		user = userService.findById(id);
		return Action.SUCCESS;
	}
	
	/**
	 * Get the ID of the user
	 * @return The ID of the user 
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * Set the ID of the user
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return All users of the application
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * Set all users of the application
	 * @param list A new list of users
	 */
	public void setUsers(List<User> list) {
		this.users = list;
	}

	/**
	 * Method that get an user.
	 * @return An user.
	 */
	public User getUser() {
		return user;
	}

	/**
	 * Method that set an user.
	 * @param user An user.
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * Method that get an e-mail.
	 * @return An e-mail.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Method that set an e-mail.
	 * @param email An e-mail.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

}
